/*
 * Copyright 2008 Jeff Dwyer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.apress.progwt.client.domain;

//Generated Jul 18, 2006 12:44:47 PM by Hibernate Tools 3.1.0.beta4

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.apress.progwt.client.domain.forum.ForumTopic;
import com.apress.progwt.client.domain.generated.AbstractUser;

/**
 * User generated by hbm2java
 */

/**
 * Extended by ServerSideUser on the Server to implement UserDetails,
 * Since getting the acegisecurity jar into client side land was a no go.
 * 
 */
public class User extends AbstractUser implements Serializable, Loadable,
        ForumTopic {

    /**
     * getter speed optimization
     */
    private transient List<ProcessType> nonStatusProcessTypes;
    /**
     * getter speed optimization
     */
    private transient List<ProcessType> statusProcessTypes;

    public User() {
        setEnabled(true);
        setSupervisor(false);

    }

    public boolean isAccountNonExpired() {
        return true;
    }

    public boolean isAccountNonLocked() {
        return true;
    }

    public boolean isCredentialsNonExpired() {
        return true;
    }

    // @Override
    public String toString() {
        return "|" + getId() + ":" + getUsername() + "|";
    }

    // /**
    // * TODO PEND HIGH
    // */
    // public String getNickname() {
    // if (isOpenID()) {
    // String half = getUsername().replaceAll("http://", "");
    // return half.replaceAll("/", "");
    // }
    // return getUsername();
    // }

    private boolean isOpenID() {
        return getPassword() == null;
    }

    public void addRanked(Application schoolAndAppProcess) {
        schoolAndAppProcess.setUser(this);
        getSchoolRankings().add(schoolAndAppProcess);
    }

    public void addRanked(int rank, Application sap) {
        sap.setUser(this);
        getSchoolRankings().remove(sap);
        getSchoolRankings().add(rank, sap);
    }

    public int getPriority(RatingType ratingType) {
        Integer priority = getPriorities().get(ratingType);
        if (priority == null) {
            return 5;
        }
        return priority;
    }

    public List<ApplicationAndScore> getPrioritizedRankings() {
        List<ApplicationAndScore> rtn = new ArrayList<ApplicationAndScore>();

        for (Application application : getSchoolRankings()) {
            rtn.add(application.getAppAndScore(getRatingTypes(), this));
        }

        Collections.sort(rtn);

        return rtn;
    }

    /**
     * Cache this list to speed things up
     * 
     * @return
     */
    public List<ProcessType> getNonStatusProcessTypes() {
        if (null == nonStatusProcessTypes) {
            nonStatusProcessTypes = getProcessTypes(false);
        }
        return nonStatusProcessTypes;
    }

    /**
     * Cache this list to speed things up
     * 
     * @return
     */
    public List<ProcessType> getStatusProcessTypes() {
        if (null == statusProcessTypes) {
            statusProcessTypes = getProcessTypes(true);
        }
        return statusProcessTypes;
    }

    private List<ProcessType> getProcessTypes(boolean status) {
        List<ProcessType> rtn = new ArrayList<ProcessType>();
        for (ProcessType processType : getProcessTypes()) {
            if (status == processType.getStatus_order() > 0) {
                rtn.add(processType);
            }
        }
        return rtn;
    }

    public String getForumDisplayName() {
        return getNickname();
    }

    public ForumPost getForumPost() {
        return null;
    }

    public ForumPost getReplyInstance(User author, String title,
            String text, ForumPost thread) {
        return new UserForumPost(this, author, title, text, thread);
    }

    public String getUniqueForumID() {
        return "User" + ForumTopic.SEP + getId();
    }

    public boolean doThreadListView() {
        return false;
    }
}
